﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using MensUnoShop.Models;
using MensUnoShop.Areas.Admin.Models;
using MensUnoShop.Areas.Admin.Filters;

namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    public class KeywordSetupController : Controller
    {
        public ActionResult List()
        {
            List<UHotKey> hklist = KeyWordFactory.GetManagerHotKeyList();
            ViewData["colorlist"] = System.Configuration.ConfigurationManager.AppSettings["HotKeyColor"];
            ViewData["searchkeylist"] = hklist.OrderByDescending(c=>c.HTcount).Take(Config.hotkeycount).ToList();
            return View();
        }

        public JsonResult api_getkeywordlist()
        {
            XElement hklist = KeyWordFactory.GetHotKeyList();
            if(hklist.HasElements)
                return Json(new { code = "200", keyword = hklist.Elements("HotKey").Select(m => m.Element("Keyword").Value), color = hklist.Elements("HotKey").Select(m => m.Attribute("Color").Value) },JsonRequestBehavior.AllowGet);
            else
                return Json(new { code = "500",response = "請稍後再試!!"});
        }

        public JsonResult api_addkeyword(string keyword)
        {
            string msg = string.Empty;
            if (KeyWordFactory.AddKeyword(keyword, ref msg))
            {
                XElement hklist = KeyWordFactory.GetHotKeyList();
                if (hklist.HasElements)
                    return Json(new { code = "200", keyword = hklist.Elements("HotKey").Select(m => m.Element("Keyword").Value), color = hklist.Elements("HotKey").Select(m => m.Attribute("Color").Value) }, JsonRequestBehavior.AllowGet);
                else
                    return Json(new { code = "501", response = "請稍後再試!!" });
            }
            else
                return Json(new { code = "500", response = msg });
        }

        public JsonResult api_delkeyword(string keyword)
        {
            string msg = string.Empty;
            if (KeyWordFactory.DelKeyword(keyword, ref msg))
            {
                XElement hklist = KeyWordFactory.GetHotKeyList();
                if (hklist.HasElements)
                    return Json(new { code = "200", keyword = hklist.Elements("HotKey").Select(m => m.Element("Keyword").Value), color = hklist.Elements("HotKey").Select(m => m.Attribute("Color").Value) }, JsonRequestBehavior.AllowGet);
                else
                    return Json(new { code = "501", response = "請稍後再試!!" });
            }
            else
                return Json(new { code = "500", response = msg });
        }

        public JsonResult api_setkeywordcolor(string keyword, int cid)
        {
            string msg = string.Empty;
            if (KeyWordFactory.SetKeywordColor(keyword, cid, ref msg))
            {
                XElement hklist = KeyWordFactory.GetHotKeyList();
                if (hklist.HasElements)
                    return Json(new { code = "200", keyword = hklist.Elements("HotKey").Select(m => m.Element("Keyword").Value), color = hklist.Elements("HotKey").Select(m => m.Attribute("Color").Value) }, JsonRequestBehavior.AllowGet);
                else
                    return Json(new { code = "501", response = "請稍後再試!!" });
            }
            else
                return Json(new { code = "500", response = msg });
        }
    }
}
